package api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gotd/td/core"
)

func Login(c *gin.Context) {
	//param := model.ApplyLogin{}
	//if err := c.BindJSON(&param); err != nil {
	//	ErrResp(c, err)
	//	return
	//}
	var loginUserId = ""
	user := &core.TgUser{}
	if err := user.Run(core.ApiIdRaw, core.ApiHash, "", func(userId string) {
		core.UserManagerMap.Store(userId, user)
		fmt.Println("连接Tg成功:", userId)
		fmt.Println("登录启动账号:", userId)
		loginUserId = userId
	}); err != nil {
		// user.run --> NewClient error
		fmt.Println("user.run -->", err.Error())
	}
	//var storeKey = core.UserManagerKey(param.PhoneNumber)
	//go func() {
	//	if val, ok := core.UserManagerMap.Load(storeKey); ok && val != nil {
	//		tgUser := val.(*core.TgUser)
	//		if tgUser.IsRunning {
	//			fmt.Println("已经登录:", storeKey)
	//			return
	//		}
	//	}
	//	if err := user.Run(core.ApiIdRaw, core.ApiHash, param.PhoneNumber, func(phoneNumber string) {
	//		core.UserManagerMap.Store(storeKey, user)
	//		fmt.Println("连接Tg成功:", storeKey)
	//	}); err != nil {
	//		// user.run --> NewClient error
	//		fmt.Println("user.run -->", err.Error())
	//	}
	//}()
	OkResp(c, loginUserId)
}
